//
// Created by Administrator on 2022/1/26.
//

#ifndef CPP_LEARNING_LEETCODE239_H
#define CPP_LEARNING_LEETCODE239_H
#include <vector>
using namespace std;
#include <math.h>
#include <iostream>
#include <deque>

//    5   4   3   2
// 单调递减队列
class MyQueue{
private :
    deque<int> que;

public:
    void push(int v){
        while (!que.empty() && (que.back() < v)){
            que.pop_back();
        }
        que.push_back(v);
    }

    void pop(){
        que.pop_front();
    }

    int front(){
        return que.front();
    }
};
class Solution239 {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        vector<int> result;
        MyQueue myQueue;
        for(int i=0;i<k;i++){
            myQueue.push(nums[i]);
        }

        for(int i=k; i<nums.size(); i++){
             result.push_back(myQueue.front());
             // 先移除
             if( myQueue.front() == nums[i-k]){
                 myQueue.pop();
             }
             // 后添加
             myQueue.push(nums[i]);
        }
        result.push_back(myQueue.front());
        return result;
    }
};

void test239(){
    Solution239 s;
    vector<int> nums= {1,3,-1,-3,5,3,6,7};
    s.maxSlidingWindow(nums, 3);

}
#endif //CPP_LEARNING_LEETCODE209_H
